import java.io.*;
import java.util.UUID;

/**
 * @Package: PACKAGE_NAME
 * @author: Jounghu
 * @date: 2018/7/28 10:31
 * @version: V1.0
 */
public class FileUtils {

    private static final String FILE_SUBFIX = ".txt";

    public static String readFromFile(String path, String dict) throws IOException {

        File outFile = null;
        if (path.contains(dict)) {
            outFile = new File(dict, UUID.randomUUID() + FILE_SUBFIX);
        } else {
            outFile = new File(dict, path.substring(path.lastIndexOf(File.separator)));
        }


        File readFile = new File(path);
        OutputStream os = null;
        BufferedWriter bw = null;
        InputStream is = null;
        BufferedReader bf = null;

        try {

            os = new FileOutputStream(outFile);
            bw = new BufferedWriter(new OutputStreamWriter(os));

            is = new FileInputStream(readFile);
            bf = new BufferedReader(new InputStreamReader(is));
            String str;
            while ((str = bf.readLine()) != null) {
                if (str.charAt(0) == '1') {
                    str = str.replaceFirst("1", "");
                }
                // 写入文件
                bw.write(str + "\r\n");
            }
            bw.flush();

        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("文件不存在！");
        } catch (IOException e) {
            throw new IOException("读取文件错误！");
        } finally {
            if (is != null) {
                is.close();
            }

            if (bf != null) {
                bf.close();
            }

            if (os != null) {
                os.close();
            }

            if (bw != null) {
                bw.close();
            }

        }
        return outFile.getAbsolutePath();
    }


    public static void useCMDCommand(String path) throws IOException {

        Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", path});

    }
}
